package com.yb.bussiness.member;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yb.base.dao.BaseDao;
import com.yb.base.dao.IBaseDao;
import com.yb.domain.member.TpSuggest;
import com.yb.util.comm.DateTimeUtil;
import com.yb.util.framework.PageUtil;

public class SuggestServiceImpl implements SuggestService {

	private IBaseDao<TpSuggest, Long> dao;

	public SuggestServiceImpl() {
		dao = new BaseDao<TpSuggest, Long>(TpSuggest.class);
	}
	
	public PageUtil getList(Map<String, String> map, int start, int limit) {
		if (map == null || map.size() == 0)
		return null;
		PageUtil pu = new PageUtil();
		String find_sql ="SELECT * FROM t_p_suggest where 1=1 ";
		String countSql ="SELECT count(*) FROM t_p_suggest where 1=1 ";
		try {
			if (!"".equals(map.get("c_startTime"))){
				if(!"".equals(map.get("c_endTime"))){
					find_sql += "  AND  createTime BETWEEN '"+map.get("c_startTime")+"' AND '"+map.get("c_endTime")+"'";
					countSql += "  AND  createTime BETWEEN '"+map.get("c_startTime")+"' AND '"+map.get("c_endTime")+"'";
				}else{
					find_sql += "  and createTime>='"+map.get("c_startTime")+"'";
					countSql += "  and createTime>='"+map.get("c_startTime")+"'";
				}
			}else if(!"".equals(map.get("c_endTime"))){
				find_sql += "  and createTime<='"+map.get("c_endTime")+"'";
				countSql += "  and createTime<='"+map.get("c_endTime")+"'";
			}
			if (!"".equals(map.get("a_startTime"))){
				if(!"".equals(map.get("a_endTime"))){
					find_sql += "  AND  acceptTime BETWEEN '"+map.get("a_startTime")+"' AND '"+map.get("a_endTime")+"'";
					countSql += "  AND  acceptTime BETWEEN '"+map.get("a_startTime")+"' AND '"+map.get("a_endTime")+"'";
				}else{
					find_sql += "  and acceptTime>='"+map.get("a_startTime")+"'";
					countSql += "  and acceptTime>='"+map.get("a_startTime")+"'";
				}
			}else if(!"".equals(map.get("a_endTime"))){
				find_sql += "  and acceptTime<='"+map.get("a_endTime")+"'";
				countSql += "  and acceptTime<='"+map.get("a_endTime")+"'";
			}
			if (null != map.get("title") && !"".equals(map.get("title"))) {
				find_sql += "  and title like '%" + map.get("title") + "%'";
				countSql += "  and title like '%" + map.get("title") + "%'";
			}
			
			List<Object[]> countList = dao.findObjectArryList(countSql.toString());
			List<TpSuggest> newlist = null;
			if (null != countList && countList.size() > 0) {
				Object[] countAry = countList.get(0);
				pu.setTotalRecords(new Integer(countAry[0].toString()));
				List<TpSuggest> list = dao.findList(find_sql+" ORDER BY createTime desc,acceptTime desc limit ?,?",new Object[]{(start-1)*limit,limit});
				if (list != null && list.size() > 0) {
					newlist = new ArrayList<TpSuggest>();
					for (TpSuggest tp : list) {
						if(null!=tp.getCreateTime())
							tp.setCreatTimeStr(DateTimeUtil.switchDateToString(tp.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
						if(null!=tp.getAcceptTime())
							tp.setAcceptTimeStr(DateTimeUtil.switchDateToString(tp.getAcceptTime(),"yyyy-MM-dd HH:mm:ss"));
						newlist.add(tp);
					}
				}
			} else {
				pu.setTotalRecords(0);
			}
			if (null == newlist)
				newlist = new ArrayList<TpSuggest>();
			pu.setList(newlist);
			pu.setPageNo(start);
			pu.setPageSize(limit);
			return pu;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		}

	public boolean deleteByIdString(String idString) {
		boolean isFlag = true;
		if (idString == null || "".equals(idString))
			return false;
		String sql = "";
		String[] delteid = idString.split("_");
		try {
			for (int i = 0; i < delteid.length; i++) {
				sql = "delete from t_p_suggest where id=" + delteid[i];
				dao.delete(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return isFlag;
	}

	public boolean saveOrUpdate(TpSuggest suggest) {
		try {
			dao.saveOrUpdate(suggest);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public TpSuggest read(long id) {
		return dao.read(id);
	}
	
	public void saveReply(Long id, String replyContent) {
		String sql = "update t_p_suggest set replayContent = ? where id = ?";
		dao.update(sql, new Object[] { replyContent, id });
	}

	public boolean onAutding(String idString,String type) {
		boolean isFlag = true;
		if (idString == null || "".equals(idString))
			return false;
		String sql = "";
		try {
			if("1".equals(type))
				sql = "update t_p_suggest set isAccept = ?,acceptTime=now() where id = ?";
			else
				sql = "update t_p_suggest set isAccept = ?,acceptTime=null where id = ?";
				dao.update(sql, new Object[] { type, idString });
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return isFlag;
	}
}
